<%@ page contentType="text/html; charset=UTF-8"%>
<%@include file="/html/portlet/chart/template/init.jsp"%>

<%
String redirect = ParamUtil.getString(request, "redirect");	
ChartTemplate template = null;
	long chartTemplateId = ParamUtil.getLong(request, "chartTemplateId");
	if (chartTemplateId > 0) {
		template =ChartTemplateLocalServiceUtil.getChartTemplate(chartTemplateId);
	}
%>
<portlet:actionURL var="editChartTemplateURL" name="addOrUpdateChartTemplate"/>

<liferay-ui:header backURL="<%=redirect%>"
	localizeTitle="<%=(template == null)%>"
	title='<%=(template == null) ? "chart.manage.template.new" : template.getName()%>' />
<aui:form action="<%= editChartTemplateURL %>" method="post" name="fm" onSubmit='<%= "event.preventDefault(); " + renderResponse.getNamespace() + "saveTemplate();" %>'>
	<aui:input name="<%= Constants.CMD %>" type="hidden" value="<%= (template != null) ? Constants.UPDATE : Constants.ADD %>" />
	<aui:model-context bean="<%=template%>" model="<%=ChartTemplate.class%>" />
	<aui:input type="hidden" name="chartTemplateId" />
	<aui:input name="saveAndContinue" type="hidden" value="<%= false %>" />
	<aui:input name="script" type="hidden" />
	
	<aui:input name="name" required="true"/>
	<aui:input name="description" type="textarea" />
	<liferay-ui:panel-container extended="false" persistState="<%= true %>" >
		<liferay-ui:panel collapsible="<%=true%>" extended="<%=false%>"
					id="<portlet:namespace />script" persistState="<%=true%>"
					title="script">
			<div id="<portlet:namespace />editorContainer">
				<div class="lfr-rich-editor" id="<portlet:namespace />richEditor"></div>
			</div>
			<aui:script use="aui-ace-editor">
				var editorContentElement = A.one('#<portlet:namespace />script');
				var richEditor ;
				A.on(
						'domready',
						function(event) {
							richEditor = new A.AceEditor({
								boundingBox : '#<portlet:namespace />richEditor',
								height : 400,
								mode : 'php',
								width : '100%'
							}).render();
							if (editorContentElement) {
								setEditorContent(editorContentElement.val());
							}
							Liferay.on(
									'<portlet:namespace />saveTemplate',
									function(event) {
										editorContentElement.val(getEditorContent());
									}
							);
						}
				);
				function setEditorContent(content){
					richEditor.getSession().setValue(content);
				}
				function getEditorContent() {
					var content = richEditor.getSession().getValue();
					return content;
				}
			</aui:script>
		</liferay-ui:panel>
		<liferay-ui:panel collapsible="<%=true%>" extended="<%=false%>"
					id="<portlet:namespace />script-param" persistState="<%=true%>"
					title="chart.manage.template.script.param">
				<liferay-util:include page="/html/portlet/chart/template/edit_template_script_param.jsp" servletContext="<%= application %>"></liferay-util:include>	
		</liferay-ui:panel>
	</liferay-ui:panel-container>
	

	<aui:button-row>
		<aui:script>
		Liferay.after(
			'<portlet:namespace />saveTemplate',
			function() {
				submitForm(document.<portlet:namespace />fm);
			}
		);
	</aui:script>
	<%
	String taglibOnClick = "Liferay.fire('" + liferayPortletResponse.getNamespace() + "saveTemplate');";
	%>
		<aui:button onClick="<%= taglibOnClick %>" primary="<%= true %>" value='<%= LanguageUtil.get(pageContext, "save") %>' />
		<aui:button href="<%=redirect %>" value='<%= LanguageUtil.get(pageContext, "cancel") %>'/>
	</aui:button-row>
</aui:form>
